package etc;

import java.io.IOException;
import java.util.HashMap;

import etc.helper.IniFile;


@SuppressWarnings("javadoc")
public class Config
{

	private static final String FILENAME = "config.ini";
	private static HashMap<String, String> data = new HashMap<String, String>();

	public static void set(String key, String value)
	{
		data.put(key, value);
	}
	
	public static void setAsBoolean(String key, boolean value)
	{
		set(key, value ? "true" : "false");
	}
	
	public static void setAsInt(String key, int value)
	{
		set(key, Integer.toString(value));
	}
	
	public static void setAsArray(String key, String[] value)
	{
		// Check whether the value array contains data
		if (value.length > 0)
		{
			// Create a string builder and add the first value
			StringBuilder sb = new StringBuilder();
			sb.append(value[0]);

			// Iterate the next values
			for (int i=1; i < value.length; i++)
			{
				// Append the delimiter and the value
				sb.append(",");
				sb.append(value[i]);
			}

			// Add the array
			set(key, sb.toString());
		}
	}

	public static String get(String key)
	{
		return data.get(key);
	}
	
	public static boolean getAsBoolean(String key)
	{
		return get(key) == "true" || get(key) == "1";
	}

	public static int getAsInt(String key)
	{
		return Integer.parseInt(get(key));
	}

	public static String[] getAsArray(String key)
	{
		return get(key).split(",");
	}

	public static boolean has(String key)
	{
		return data.containsKey(key);
	}
	
	public static boolean hasElements()
	{
		return !data.isEmpty();
	}

	public static void save()
	{
		// TODO
		IniFile.save("config2.ini", data); // TODO: FILENAME
	}

	public static void load()
	{
		try {
			data = IniFile.load(FILENAME);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args)
	{
		Config.load();
		Config.save();
	}

}